package de.herberlin.pss.ui;

import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import de.herberlin.pss.ServiceLocator;

public class AppMenu {
	
	private ActionListener listener=null;
	
	public JMenuBar getAppMenu(ActionListener listener) {
		this.listener=listener;
		JMenuBar menuBar=new JMenuBar();
		menuBar.add(createMenu("file", new String[]{"choose","exit"}));
		menuBar.add(createMenu("edit", new String[]{"add","delete"}));
		// menuBar.setHelpMenu(createMenu("help", new String[]{"help","about"}));
		menuBar.add(createMenu("help", new String[]{"help","_about"}));
		
		return menuBar;
	}
	

	private JMenu createMenu(String title, String[] entries) {
		
		JMenu menu=new JMenu(getText(title));
		for (String key : entries) {
			boolean enabled=true;
			if (key.startsWith("_")) {
				key=key.substring(1);
				enabled=false;
			}
			JMenuItem item=new JMenuItem(getText(key));
			item.setEnabled(enabled);
			item.setActionCommand(key);
			if (listener !=null) {
				item.addActionListener(listener);
			}
			menu.add(item);
			
		}
		return menu;
	}
	
	
	private static String getText(String key) {
		return ServiceLocator.getText("Menu."+key);
	}
}
